package com.ruicar.afs.cloud.basic.common.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.ruicar.afs.cloud.common.core.entity.BaseEntity;
import com.ruicar.afs.cloud.common.modules.contract.enums.AdjustTypeEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.AssetTypeEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.CalculateType;
import com.ruicar.afs.cloud.common.modules.contract.enums.DiscountType;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author: Lee
 * @date 2020-05-29 09:31:34 
 * @date 2020-05-29 09:31:34
 * @description 合同金融协议表
 */
@Data
@Accessors(chain = true)
@TableName(value ="basic_financial_agreement", autoResultMap = true)
public class BasicFinancialAgreement extends BaseEntity<BasicFinancialAgreement> {

	/** 合同号码 */
	@ApiModelProperty("合同号码")
	private String contractNo;
	/** 车辆ID */
	@ApiModelProperty("车辆ID")
	@JsonSerialize(using = ToStringSerializer.class)
	private Long carId;
	/** 产品ID */
	@ApiModelProperty("产品ID")
	@JsonSerialize(using = ToStringSerializer.class)
	private Long productId;
	/** 融资费用类型 车款，附加贷 */
	@ApiModelProperty("融资费用类型 车款，附加贷")
	private AssetTypeEnum costType;
	/** 合同金额 */
	@ApiModelProperty("合同金额")
	private BigDecimal contractAmt;
	/** 客户利率/贴息后利率 */
	@ApiModelProperty("客户利率/贴息后利率")
	private BigDecimal custRate;
	/** 结算利率 */
	@ApiModelProperty("结算利率")
	private BigDecimal settleRate;
	/** 上调/下调 枚举值：上调，下调 */
	@ApiModelProperty("上调/下调 枚举值：上调，下调")
	private AdjustTypeEnum adjustType;
	/** 调整基点 */
	@ApiModelProperty("调整基点")
	private Integer basicPoint;
	/** 首付比例 */
	@ApiModelProperty("首付比例")
	private BigDecimal downPayScale;
	/** 首付金额 */
	@ApiModelProperty("首付金额")
	private BigDecimal downPayAmt;
	/** 尾付比例 */
	@ApiModelProperty("尾付比例")
	private BigDecimal tailPayScale;
	/** 尾付金额 */
	@ApiModelProperty("尾付金额")
	private BigDecimal tailPayAmt;
	/** 贴息方式 */
	@ApiModelProperty("贴息方式")
	private DiscountType discountType;
	/** 贴息总额 */
	@ApiModelProperty("贴息总额")
	private BigDecimal totalDiscountAmt;
	/** 厂商最大贴息额 */
	@ApiModelProperty("厂商最大贴息额")
	private BigDecimal maxDiscountAmt;
	/** 算法类型 */
	@ApiModelProperty("算法类型")
	private CalculateType algorithmType;
	/** 贷款金额 */
	@ApiModelProperty("贷款金额")
	private BigDecimal loanAmt;
	/** 融资期数 */
	@ApiModelProperty("融资期数")
	private Integer loanTerm;
	/** 月供金额 */
	@ApiModelProperty("月供金额")
	private BigDecimal monthPayAmt;

	/** 是否有子产品 */
	@ApiModelProperty("是否有子产品")
	private String isSubProduct;
	/** 附加金额 */
	@ApiModelProperty("附加金额")
	private BigDecimal addAmt;


	/** 附加金额 */
	@ApiModelProperty("逾期利率")
	private BigDecimal overDueRate;
	@ApiModelProperty("专项准备金")
	private BigDecimal specificProvisions;
	@ApiModelProperty("暂记账")
	private BigDecimal suspenseAccount;
	@ApiModelProperty("特须暂记账(90天以上的)")
	private BigDecimal specialTemporary;
	@ApiModelProperty("利息收入")
	private BigDecimal interestIncome;

	/**
	 * 金额类数据进行汇总
	 * @param list
	 * @return
	 */
	public BasicFinancialAgreement sum(List<BasicFinancialAgreement> list){
		BigDecimal contractAmt = new BigDecimal(0);
		BigDecimal downPayAmt = new BigDecimal(0);
		BigDecimal tailPayAmt = new BigDecimal(0);
		BigDecimal totalDiscountAmt = new BigDecimal(0);
		BigDecimal loanAmt = new BigDecimal(0);
		BigDecimal monthPayAmt = new BigDecimal(0);
		BigDecimal custRate = new BigDecimal(0);
		BigDecimal addAmt = new BigDecimal(0);
		Integer basicPoint = 0;
		Integer loanTerm = 0;
		for(BasicFinancialAgreement agreement : list) {
			contractAmt = contractAmt.add(EmptyUtils.isNotEmpty(agreement.getContractAmt())?agreement.getContractAmt():BigDecimal.ZERO);
			downPayAmt = downPayAmt.add(EmptyUtils.isNotEmpty(agreement.getDownPayAmt())?agreement.getDownPayAmt():BigDecimal.ZERO);
			tailPayAmt = tailPayAmt.add(EmptyUtils.isNotEmpty(agreement.getTailPayAmt())?agreement.getTailPayAmt():BigDecimal.ZERO);
			totalDiscountAmt = totalDiscountAmt.add(EmptyUtils.isNotEmpty(agreement.getTotalDiscountAmt())?agreement.getTotalDiscountAmt():BigDecimal.ZERO);
			loanAmt = loanAmt.add(EmptyUtils.isNotEmpty(agreement.getLoanAmt())?agreement.getLoanAmt():BigDecimal.ZERO);
			monthPayAmt = monthPayAmt.add(EmptyUtils.isNotEmpty(agreement.getMonthPayAmt())?agreement.getMonthPayAmt():BigDecimal.ZERO);
			addAmt = addAmt.add(EmptyUtils.isNotEmpty(agreement.getAddAmt())?agreement.getAddAmt():BigDecimal.ZERO);
			if(AssetTypeEnum.CAR_AMT == agreement.getCostType()){
				custRate = agreement.getCustRate();
				basicPoint = agreement.getBasicPoint();
				loanTerm = agreement.getLoanTerm();
			}
		}
		BasicFinancialAgreement agreement = new BasicFinancialAgreement();
		agreement.setContractAmt(contractAmt);
		agreement.setDownPayAmt(downPayAmt);
		agreement.setTailPayAmt(tailPayAmt);
		agreement.setTotalDiscountAmt(totalDiscountAmt);
		agreement.setLoanAmt(loanAmt);
		agreement.setMonthPayAmt(monthPayAmt);
		agreement.setCustRate(custRate);
		agreement.setBasicPoint(basicPoint);
		agreement.setLoanTerm(loanTerm);
		agreement.setAddAmt(addAmt);
		return agreement;
	}

}
